/*
 * graph2D
 * Copyright (c) 2009 Shun Moriya <shun@mnu.sakura.ne.jp>
 *
 * The MIT License
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#define INCLUDE_FROM_OBJC_C
#include "audioPlayer_impl.h"
#include "data_impl.h"
#import <AVFoundation/AVFoundation.h>

namespace Graph2D
{
	namespace Implementation
	{
		namespace AudioPlayer
		{
			void* play(const std::string& filename)
			{
				NSString* pathName = [NSString stringWithUTF8String:filename.c_str()];
				NSURL* resourceURL = [DataController getResouceUriWithString:pathName];
				assert(resourceURL);

				AVAudioPlayer* track = [[AVAudioPlayer alloc] initWithContentsOfURL:resourceURL error:nil];
				if(track == nil)
					return NULL;

				if(![track play])
				{
					[track release];
					return NULL;
				}

				return track;
			}

			void pause(void* self, const bool on)
			{
				AVAudioPlayer* player = reinterpret_cast<AVAudioPlayer*>(self);
				if(on)
					[player pause];
				else
					[player play];
			}

			void stop(void* self)
			{
				AVAudioPlayer* player = reinterpret_cast<AVAudioPlayer*>(self);
				[player stop];
			}

			void release(void* self)
			{
				AVAudioPlayer* player = reinterpret_cast<AVAudioPlayer*>(self);
				[player stop];
				[player release];
			}

			bool playing(void* self)
			{
				AVAudioPlayer* player = reinterpret_cast<AVAudioPlayer*>(self);
				return player.playing;
			}

			float addVolume(void* self, const float offset)
			{
				AVAudioPlayer* player = reinterpret_cast<AVAudioPlayer*>(self);
				player.volume += offset;
				return player.volume;
			}

			float getVolume(void* self)
			{
				AVAudioPlayer* player = reinterpret_cast<AVAudioPlayer*>(self);
				return player.volume;
			}

			void setVolume(void* self, const float vol)
			{
				AVAudioPlayer* player = reinterpret_cast<AVAudioPlayer*>(self);
				player.volume = vol;
			}

			void setLoopCount(void* self, const int numberOfLoops)
			{
				AVAudioPlayer* player = reinterpret_cast<AVAudioPlayer*>(self);
				player.numberOfLoops = numberOfLoops;
			}

			double getCurrentTime(void* self)
			{
				AVAudioPlayer* player = reinterpret_cast<AVAudioPlayer*>(self);
				NSTimeInterval timeInterval = player.currentTime;
				return static_cast<double>(timeInterval);
			}

			void setCurrentTime(void* self, const double second)
			{
				AVAudioPlayer* player = reinterpret_cast<AVAudioPlayer*>(self);
				player.currentTime = static_cast<NSTimeInterval>(second);
			}
		}
	}
}
